package com.ibeetl.cms.web;

import com.ibeetl.admin.core.annotation.Function;
import com.ibeetl.admin.core.entity.CoreUser;
import com.ibeetl.admin.core.file.FileService;
import com.ibeetl.admin.core.service.CorePlatformService;
import com.ibeetl.admin.core.util.ConvertUtil;
import com.ibeetl.admin.core.util.ValidateConfig;
import com.ibeetl.admin.core.web.JsonResult;
import com.ibeetl.cms.entity.AnnualRevenue;
import com.ibeetl.cms.service.AnnualRevenueService;
import com.ibeetl.cms.service.EnterpriseService;
import com.ibeetl.cms.web.query.AnnualRevenueQuery;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 营销 接口
 */
@Controller
public class AnnualRevenueController{

    private final Log log = LogFactory.getLog(this.getClass());
    private static final String MODEL = "/cms/annualRevenue";

    @Autowired
    CorePlatformService platformService;

    @Autowired private EnterpriseService enterpriseService;

    @Autowired private AnnualRevenueService annualRevenueService;
    
    @Autowired
    FileService fileService;
    /* 页面 */

    @GetMapping(MODEL + "/index.do")
    @Function("cms.enterprise.edit")
    @ResponseBody
    public ModelAndView index() {
        ModelAndView view = new ModelAndView("/cms/annualRevenue/index.html") ;
        view.addObject("search", AnnualRevenueQuery.class.getName());
        return view;
    }

    @GetMapping(MODEL + "/edit.do")
    @Function("cms.enterprise.edit")
    @ResponseBody
    public ModelAndView edit(Integer id,String companyName) {
        ModelAndView view = new ModelAndView("/cms/annualRevenue/edit.html");
        AnnualRevenue annualRevenue = annualRevenueService.queryById(id);
        view.addObject("annualRevenue", annualRevenue);
        view.addObject("companyName", companyName);
        return view;
    }

    @GetMapping(MODEL + "/add.do")
    @Function("cms.enterprise.edit,cms.enterprise.query")
    @ResponseBody
    public ModelAndView add() {
        CoreUser currentUser = platformService.getCurrentUser();
        ModelAndView view = new ModelAndView("/cms/annualRevenue/add.html");
        view.addObject("companyName", enterpriseService.findCompanyId(currentUser.getId()).getEnterpriseName());
        return view;
    }

    /* ajax json */

    @PostMapping(MODEL + "/list.json")
    @ResponseBody
    public JsonResult<PageQuery> list(AnnualRevenueQuery condtion,Integer userId)
    {
        PageQuery page = condtion.getPageQuery();
        condtion.setCompanyId(Math.toIntExact(userId));
        annualRevenueService.queryByCondition(page);
        return JsonResult.success(page);
    }

    @PostMapping(MODEL + "/add.json")
    @Function("cms.enterprise.edit")
    @ResponseBody
    public JsonResult add(@Validated(ValidateConfig.ADD.class)AnnualRevenue annualRevenue)
    {
        annualRevenue.setCompanyId(Math.toIntExact(platformService.getCurrentUser().getId()));
        annualRevenueService.save(annualRevenue);
        return new JsonResult().success();
    }

    @PostMapping(MODEL + "/update.json")
    @Function("cms.enterprise.edit")
    @ResponseBody
    public JsonResult<String> update(@Validated(ValidateConfig.UPDATE.class)  AnnualRevenue annualRevenue) {
        boolean success = annualRevenueService.updateTemplate(annualRevenue);
        if (success) {
            return new JsonResult().success();
        } else {
            return JsonResult.failMessage("保存失败");
        }
    }


   
    @GetMapping(MODEL + "/view.json")
    @Function("cms.enterprise.edit")
    @ResponseBody
    public JsonResult<AnnualRevenue>queryInfo(Integer id) {
        AnnualRevenue annualRevenue = annualRevenueService.queryById( id);
        return  JsonResult.success(annualRevenue);
    }

    @PostMapping(MODEL + "/delete.json")
    @Function("cms.enterprise.edit")
    @ResponseBody
    public JsonResult delete(String ids) {
        if (ids.endsWith(",")) {
            ids = StringUtils.substringBeforeLast(ids, ",");
        }
        List<Long> idList = ConvertUtil.str2longs(ids);
        annualRevenueService.batchDelAnnualRevenue(idList);
        return new JsonResult().success();
    }
    

}
